package com.sky.sd.oauth.service.impl;

import com.sky.sd.common.web.response.Result;
import com.sky.sd.common.web.response.ResultType;
import com.sky.sd.system.bean.dto.LoginUser;
import com.sky.sd.system.feign.ISysClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <br/>功能:
 * <br/>版本: 1.0
 * <br/>开发人员: 弓振
 * <br/>创建日期: 2020/6/17 14:23
 * <br/>修改日期: 2020/6/17 14:23
 * <br/>修改列表:
 */
@Service
@Slf4j
public class SkyUserDetailsService implements UserDetailsService {

    @Autowired
    private ISysClient sysClient;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("==> {}登录",userName);
        Result<LoginUser> result = sysClient.getLoginUser(userName);
        if( ResultType.SUCCESS.getCode() == result.getCode()) {
            LoginUser loginUser = result.getBean();
            if(loginUser == null) {
                throw new UsernameNotFoundException("用户名/密码错误");
            }
            return loginUser;
        } else {
            log.info("==> userName:{} 登录失败: {}", userName , result.getMsg());
            throw new UsernameNotFoundException("用户名/密码错误");
        }
    }
}
